<template>
  <div class="login">
    <div class="login_logo">
      <img src="@/assets/logo.png"
           alt=""><span>Data Manage Platform</span>
    </div>
    <div class="login-container">
      <el-form :model="loginForm"
               :rules="loginRules"
               class="login-form"
               ref="loginFormRef">
        <h3 class="title">大数据协同管理平台</h3>
        <el-form-item prop="username">
          <el-input v-model="loginForm.username"
                    type="text"
                    auto-complete="off"
                    placeholder="账号"
                    prefix-icon="el-icon-user">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password"
                    type="password"
                    auto-complete="new-password"
                    placeholder="密码"
                    @keyup.enter="submitForm"
                    prefix-icon="el-icon-goods">
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input v-model="loginForm.code"
                    auto-complete="off"
                    placeholder="验证码"
                    style="width: 63%"
                    @keyup.enter="submitForm"
                    prefix-icon="el-icon-circle-check">
          </el-input>
          <div class="login-code">
            <img :src="loginForm.codeUrl"
                 @click="getCode"
                 class="login-code-img" />
          </div>
        </el-form-item>
        <el-checkbox v-model="loginForm.rememberMe"
                     style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
        <el-form-item style="width:100%;">
          <el-button :loading="custRef.loading"
                     size="medium"
                     type="primary"
                     style="width:100%;"
                     @click.prevent="submitForm">
            <span v-if="!custRef.loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
        </el-form-item>
      </el-form>
      <!--  底部  -->
      <div class="el-login-footer">
        <span>Copyright © 2019-2021 <a href="#">bigdata.com</a> All Rights Reserved.</span>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
// 定义一个组件
import {
  defineComponent,
  onBeforeMount,
  onMounted,
  reactive,
  ref,
  unref
} from 'vue'
import { useRouter } from 'vue-router'
import store from 'store/index'

interface LoginInfo {
  username: String
  password: String
  codeUrl: String
  code: String
  uuid: String
  rememberMe: Boolean
}

export default defineComponent({
  name: 'login',
  setup(props, ctx) {
    let custRef = reactive({
      loading: false,
      redirect: undefined
    })

    // 定义响应式对象
    const loginForm: LoginInfo = reactive({
      username: 'admin',
      password: '123456',
      codeUrl: '',
      code: '',
      uuid: '',
      rememberMe: false
    })
    //{router}是全局路由对象，route= userRoute()是当前路由对象
    const router = useRouter()
    const loginFormRef = ref()
    // 生命周期函数
    onMounted(() => {
      getCode()
    })
    const getCode = () => {
      // 调用接口获取对应的验证码图片
      loginForm.codeUrl =
        ''
      loginForm.uuid = '324434433443'
    }

    // 定义校验规则
    const loginRules = reactive({
      username: [
        { required: true, trigger: 'blur', message: '用户名不能为空' }
      ],
      password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
      code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
    })
    // 重置表单
    const resetForm = () => {
      // 笨办法这么写：
      // loginForm.value.email = ''
      // loginForm.value.pass = ''
      // 明眼人这么写：
      const form = unref(loginFormRef)
      form.resetFields()
    }

    // 表单提交
    const submitForm = async () => {
      const form = unref(loginFormRef)
      if (!form) {
        return
      }
      try {
        await form.validate((valid: boolean) => {
          if (valid) {
            userlogin()
          }
        })
      } catch (err) {
        console.log(err)
      }
    }

    let userlogin = () => {
      custRef.loading = true
      // 模拟调用接口
      const proRes = store.dispatch('Login', loginForm)
      proRes
        .then((res) => {
          console.log(res)
          custRef.loading = false
          router.push({
            //传递参数使用query的话，指定path或者name都行，但使用params的话，只能使用name指定
            path: '/home',
            query: {
              num: 1
            }
          })
        })
        .catch((err) => {
          console.log(err)
          custRef.loading = false
        })
    }
    return {
      loginFormRef, // 记得导出否则无法进行ref映射，表单操作unref就会是undefined
      loginForm,
      loginRules,
      submitForm,
      custRef
    }
  }
})
</script>

<style  lang="scss" scoped>
.login {
  height: 100vh;
  background: url(../../assets/image/visual/loginBg.png);
  background-size: 100% 100%;
  .login_logo {
    width: 363px;
    height: 87px;
    padding-top: 30px;
    margin-left: 78px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 24px;
    img {
      width: 60px;
      height: 60px;
      margin-right: 6px;
    }
  }
  .login-container {
    height: 90%;
    display: flex;
    justify-content: center;
    align-items: center;
    .title {
      margin: 0px auto 30px auto;
      text-align: center;
      color: #707070;
    }

    .login-form {
      border-radius: 6px;
      background: #ffffff;
      width: 400px;
      padding: 25px 25px 5px 25px;
      .el-input {
        height: 38px;
        input {
          height: 38px;
        }
      }
      .input-icon {
        height: 39px;
        width: 14px;
        margin-left: 2px;
      }
    }
    .login-tip {
      font-size: 13px;
      text-align: center;
      color: #bfbfbf;
    }
    .login-code {
      width: 33%;
      height: 38px;
      float: right;
      img {
        cursor: pointer;
        vertical-align: middle;
      }
    }
    .el-login-footer {
      height: 40px;
      line-height: 40px;
      position: fixed;
      bottom: 0;
      width: 100%;
      text-align: center;
      color: #fff;
      font-family: Arial;
      font-size: 12px;
      letter-spacing: 1px;
    }
    .login-code-img {
      height: 38px;
    }
  }
}
</style>
